package businessServlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Text;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

import controller.Controller;

@SuppressWarnings("serial")
public class MessageToReadBusiness extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try{	
			
			
			String userName =req.getParameter("userNameBusiness");
			int orderID = Integer.parseInt(req.getParameter("orderID"));
			String messageToRead = req.getParameter("messageToRead");
			System.out.println("@#%@#% userName"+userName+ " orderID "+orderID+" messageToRead "+messageToRead);
			int result;
			
			if (messageToRead.equals("false"))
			{
				System.out.println("##false");
				result = Controller.getInstance().setMessageToRead(orderID, userName, false);
			}
			else
			{
				result = Controller.getInstance().setMessageToRead(orderID, userName, true);			
			}
			
			if (result!=0)
			{
				req.getSession().setAttribute("validErrorBiz","order not valid");	
				resp.sendRedirect("home.jsp");	
			}
			else
			{				
				//if succeed - stay there				
			}
			
		}
		catch(NumberFormatException e){
			req.getSession().setAttribute("validErrorBiz","order id is not a number");	
			resp.sendRedirect("home.jsp");			
		}
	}
}
